/**
 * fileName:com.zimo.web.ctl.SuperCtl.java
 * projectName:Zimo
 */
package com.zimo.web.ctl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

/**
 * 父控制类,定义一些公用的方法
 * 
 * @author sg.z/2013-12-19/下午3:04:32
 */
public class SuperCtl {

	/**
	 * 
	 * description :构建json对象,输出到前端页面
	 * 
	 * @return void
	 * @param response
	 * @param contentType 响应的类型,可以为空,默认为application/json
	 * @param statusCode 响应的状态码 成功：success,其它为失败,不能为空
	 * @param message 响应到前端的信息,不能为空
	 * @author sg/2013-12-19/下午3:23:40
	 */
	protected void ajaxResponse(HttpServletResponse response, String contentType,String statusCode, String message) {
		
		JSONObject json = new JSONObject();
		json.put("status",statusCode);
		json.put("msg",message);
		
		String type = contentType;
		if (StringUtils.isBlank(type)) {
			type = "application/json";
		}
		response.setCharacterEncoding("utf-8");
		response.setContentType(type);
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.write(json.toString());
		out.flush();
		out.close();

	}

}
